<?php
/**
 * 
 * @return 充值助手类
 * @author Administrator
 *
 */
class helper_interface_pay
{
	/**
	 * 
	 * @return 充值的用户对象
	 * @var Userbase
	 */
	protected $userbase = null;
	
	/**
	 * 
	 * Enter description here ...
	 * @var string|用户手机号码
	 */
	protected $mobile = null;
	/**
	 * 
	 * @return 点卡充值接口地址
	 * @var string
	 */
	const APP_URL = 'http://218.246.196.118/astgo/wap/call.php?action=pay&caller={$mobile}&cardid={$code}&cardpwd={$pass}';
	/**
	 * 
	 * @return 出错时可提取错误信息
	 * @var string
	 */
	public  $errmsg = '';
	
	/**
	 * 
	 *2011-12-30-下午04:44:09 by 460932465
	 * Enter description here ...
	 * @param string|手机号码 $mobile
	 */
	function __construct($mobile )
	{
			$this->mobile = $mobile;
			$this->userbase = Userbase::find('userphone = ?',$mobile)->getOne();
	}
	function set_usermobile($mobile)
	{
		$this->mobile = $mobile;
	}
	/**
	 * 
	 *2012-1-7-下午04:21:46 by 460932465
	 * @param 充值卡金额
	 * @return 开始奖励
	 */
	function union_send_point($card_money)
	{
		$B = 10; //1RMB=10积分
		//奖励给目标账号
		//先奖励第一级
		$union_set_1 = Unionsetting::find('userlevel = 1')->getOne();
		if ($union_set_1->id())
		{
			//如果赠送比例>0
			if ($union_set_1->sendpoint>0)
			{
				$point = floor($B * $card_money * ($union_set_1->sendpoint / 100)); //取整 不大于数字 舍去小数点
				QDB::getConn()->startTrans();
				try {
						$this->userbase->userpoint = $this->userbase->userpoint + $point; //开始加分
						$this->userbase->save(0,'update');
						Logs::_log($this->userbase->userid, array('ns'=>'default','controller'=>'interface','action'=>'sendpoint','module'=>'default'), "成功充值{$card_money},赠送积分:{$point}");
						QDB::getConn()->completeTrans(true);		
				} catch (Exception $e) {
						QDB::getConn()->completeTrans(false);
				}
			}
		}
		$cur_user = $this->userbase;
		//再奖励4级
		for ($i=2;$i<=5;$i++)
		{
			$cur_user = $cur_user->parent;
			$union_set = Unionsetting::find('userlevel = ?',$i)->getOne();
				if ($union_set->id() && $cur_user->id())
				{
					$point = floor($B * $card_money * ($union_set->sendpoint / 100)); //取整
					QDB::getConn()->startTrans();
					try {
					$cur_user->userpoint = $cur_user->userpoint + $point;
					$cur_user->save(0,'update');
					Logs::_log($cur_user->userid, array('ns'=>'default','controller'=>'interface','action'=>'sendpoint','module'=>'default'), "下级用户:{$this->userbase->userphone}成功充值{$card_money},赠送积分:{$point}");
					QDB::getConn()->completeTrans(true);
					} catch (Exception $e) {
					QDB::getConn()->completeTrans(false);	
					}
			 	}
		}
	}
	/**
	 * 
	 *2012-1-3-下午12:53:57 by 460932465
	 * @access 使用卡密充值，支持站外卡
	 * @return 成功返回TRUE|FALSE
	 * @param 用户手机号码 $userphone
	 * @param 卡号 $code
	 * @param 密码 $pass
	 */
	function use_card($code,$pass)
	{
					//替换卡号
					$url = str_replace('{$code}',$code,self::APP_URL);
					//替换卡密
					$url = str_replace('{$pass}',$pass,$url);
					//替换手机
					$url = str_replace('{$mobile}',$this->_getUserPhone(),$url);
					
					//开始提交
					$result = Helper_Common::curl($url);
					$result = Helper_Common::get_split('text-align:center">','</body></html>',$result);
					$check = $this->_check_paystatus($result);
					if ($result != '' && $check === true)
					{
						$this->union_send_point(floor($result));
						//成功
						return true;
					}else
					{
						//失败
						$this->errmsg = $check;
						return false;
					}
	}
	/**
	 * 
	 *2011-12-30-下午04:49:29 by 460932465
	 * @return 卡密充值,成功返回true，失败false
	 * @param productcard $productcard
	 */
	function pay_card($productcard)
	{
		
		if($productcard instanceof  QDB_ActiveRecord_Abstract)
		{
			if (isset($productcard->code) && isset($productcard->pass))
			{
				if($productcard->status == 1)
				{
					$this->errmsg = '该卡无效';
					return false;
				}else if(is_null($this->userbase) && is_null($this->mobile))
				{
					$this->errmsg = '没有设置用户对象';
					return false;
				}else
				{
					//替换卡号
					$url = str_replace('{$code}',$productcard->code,self::APP_URL);
					//替换卡密
					$url = str_replace('{$pass}',$productcard->pass,$url);
					//替换手机
					$url = str_replace('{$mobile}',$this->_getUserPhone(),$url);
					
					//开始提交
					$result = Helper_Common::curl($url);
					$result = Helper_Common::get_split('text-align:center">','</body></html>',$result);
					
					$check = $this->_check_paystatus($result);
					if ($result != '' && $check === true)
					{
						QDB::getConn()->startTrans();
						//充值成功 更改状态
						try {
							
							$productcard->status = 1; //更改为已经使用
							$productcard->result = $result; //更改接口返回内容
							$productcard->gettime = time(); //更改提取时间
							$productcard->userid = $this->_getUserPhone(); //更改使用用户的电话
							$productcard->orderid = '-1'; //为用户绑定
							$productcard->save(0,'update');
							QDB::getConn()->completeTrans(true);
							return true;
						} catch (Exception $e) {
							QDB::getConn()->completeTrans(false);
							return false;
						}
					}else
					{
							QDB::getConn()->startTrans();
						//充值成功 更改状态
						try {
							$productcard->status = 1; //更改为已经使用
							$productcard->gettime = time(); //更改提取时间
							$productcard->result = $result;
							$productcard->userid = $this->_getUserPhone(); //更改使用用户的电话
							$productcard->orderid = '-1'; //为用户绑定
							$productcard->save(0,'update');
							QDB::getConn()->completeTrans(true);
						//	return true;
						} catch (Exception $e) {
							QDB::getConn()->completeTrans(false);
						//	return false;
						}
						$this->errmsg = $check;
						return false;
					}
					
				}
				
				
			}else
			{
				$this->errmsg = '传入参数必须是充值卡对象';
				return false;
			}
			
		}else
		{
			$this->errmsg = '传入参数必须是充值卡记录类型(QDB_ActiveRecord_Abstract)';
			return false;
		}
	}
	protected function _check_paystatus($string)
	{
		$Err_Msg = array(
			'-7'=>'卡不存在',
			'-6'=>'已经被充值',
			'-5'=>'卡密不正确',
			'-4'=>'卡被禁用或金额小于0',
			'-3'=>'开户数据库输入错误',
			'-2'=>'不是同一代理',
			'-1'=>'充值操作数据库写入错误'
		);
		foreach ($Err_Msg as $k=>$v)
		{
			if ($k == $string)
			{
				return $v;
				break;
			}
		}
		//没有相应错误返回成功
		return true;
	}
	protected function _getUserPhone()
	{
		if (is_null($this->userbase))
		{
			return $this->mobile;
		}else
		{
			return $this->userbase->userphone;
		}
		
		
	}
}
?>